package com.ds.plugins.action;

import com.ds.annotation.Aggregation;
import com.ds.annotation.AggregationType;
import com.ds.annotation.Split;
import com.ds.annotation.UserSpace;
import com.ds.common.JDSException;
import com.ds.common.logging.ChromeProxy;
import com.ds.config.ErrorResultModel;
import com.ds.config.ResultModel;
import com.ds.context.JDSActionContext;
import com.ds.esd.annotation.CustomAnnotation;
import com.ds.esd.annotation.MenuBarMenu;
import com.ds.esd.annotation.event.CustomBeforInvoke;
import com.ds.esd.annotation.event.CustomOnExecueSuccess;
import com.ds.esd.annotation.field.APIEventAnnotation;
import com.ds.esd.annotation.menu.CustomMenuType;
import com.ds.esd.annotation.ui.RequestPathEnum;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.engine.ESDFacrory;
import com.ds.esd.engine.ProjectVersion;
import com.ds.web.RemoteConnectionManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = {"/action/build/"})
@Aggregation(type = AggregationType.MENU,userSpace = UserSpace.SYS)
public class BuildAction {


    @RequestMapping(value = {"rebuildCustomModule"}, method = {RequestMethod.POST})
    @APIEventAnnotation(customRequestData = {RequestPathEnum.SPA_PROJECTNAME},   onExecuteSuccess = {CustomOnExecueSuccess.MESSAGE}
            , beforeInvoke = CustomBeforInvoke.BUSY)
    @CustomAnnotation(index = 0, caption = "编译", imageClass = "spafont spa-icon-coin")
    @ResponseBody
    public ResultModel<Boolean> rebuildCustomModule(String projectName) {
        ResultModel resultModel = new ResultModel();
        ChromeProxy chrome = getCurrChromeDriver();
        try {
            ProjectVersion version = ESDFacrory.getAdminESDClient().getProjectVersionByName(projectName);
            Map map = new HashMap();
            map.put("projectId", version.getProject().getId());
            DSMFactory.getInstance().compileProject(projectName);
            ESDFacrory.getAdminESDClient().buildCustomModule(projectName, null, null, map, chrome);
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return resultModel;
    }

    @RequestMapping(value = {"javaBuild"}, method = {RequestMethod.POST})
    @APIEventAnnotation(customRequestData = {RequestPathEnum.SPA_PROJECTNAME},   onExecuteSuccess = {CustomOnExecueSuccess.MESSAGE}
            , beforeInvoke = CustomBeforInvoke.BUSY)
    @CustomAnnotation(index = 1, caption = "混合编译", imageClass = "spafont spa-icon-moveforward")
    @ResponseBody
    public ResultModel<Boolean> javaBuild(String projectName, String packageName) {
        ResultModel resultModel = new ResultModel();
        ChromeProxy chrome = getCurrChromeDriver();
        try {

            if (packageName != null && !packageName.equals("")) {
                List names = Arrays.asList(new String[]{packageName});
                ESDFacrory.getAdminESDClient().delFile(names, projectName);
            }
            DSMFactory.getInstance().compileProject(projectName);
            ESDFacrory.getAdminESDClient().buildCustomModule(projectName, packageName, null, null, chrome);
            ESDFacrory.getInstance().reload();


        } catch (Exception e) {
            e.printStackTrace();
            chrome.printError(e.getMessage());
            ((ErrorResultModel) resultModel).setErrdes(e.getMessage());
        }
        return resultModel;
    }


    @RequestMapping(value = {"split2"})
    @Split
    @CustomAnnotation(index = 2)
    @ResponseBody
    public ResultModel<Boolean> split2() {
        ResultModel resultModel = new ResultModel();
        return resultModel;
    }

    private void exportProject(String projectName, ChromeProxy chrome, boolean deploy, boolean download) {
        try {
            ESDFacrory.getAdminESDClient().exportProject(projectName, chrome, deploy, download);
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            chrome.execScript("xui.free('export')");
        }

    }

    @RequestMapping(value = {"export"}, method = {RequestMethod.POST})
    @APIEventAnnotation(customRequestData = {RequestPathEnum.SPA_PROJECTNAME})
    @CustomAnnotation(index = 3, caption = "打包发布", imageClass = "spafont spa-icon-package")
    @ResponseBody
    public ResultModel<Boolean> export(String projectName) {
        ChromeProxy defaultChrome =  getCurrChromeDriver();
        if (projectName != null) {
            RemoteConnectionManager.getConntctionService(projectName).execute(new Runnable() {
                @Override
                public void run() {
                    exportProject(projectName, defaultChrome, false, false);
                }
            });
        }
        ResultModel resultModel = new ResultModel();
        return resultModel;
    }


    @RequestMapping(value = {"download"}, method = {RequestMethod.POST})
    @APIEventAnnotation(customRequestData = {RequestPathEnum.SPA_PROJECTNAME})
    @CustomAnnotation(index = 3, caption = "打包下载", imageClass = "spafont spa-icon-select1")
    @ResponseBody
    public ResultModel<Boolean> download(String projectName) {
        ChromeProxy defaultChrome =  getCurrChromeDriver();
        if (projectName != null) {
            RemoteConnectionManager.getConntctionService(projectName).execute(new Runnable() {
                @Override
                public void run() {
                    exportProject(projectName, defaultChrome, false, true);
                }
            });
        }
        ResultModel resultModel = new ResultModel();
        return resultModel;
    }



    @Split
    @CustomAnnotation(index = 4)
    @ResponseBody
    public ResultModel<Boolean> split() {
        ResultModel resultModel = new ResultModel();
        return resultModel;
    }


    @RequestMapping(method = RequestMethod.POST, value = "Public")
    @CustomAnnotation(index = 5)
    @MenuBarMenu(menuType = CustomMenuType.SUB, caption = "发布", imageClass = "spafont spa-icon-newprj")
    public PublicAction getPublicAction() {
        return new PublicAction();
    }


    public ChromeProxy getCurrChromeDriver() {
        ChromeProxy chrome  = JDSActionContext.getActionContext().Par(ChromeProxy.class);
        return chrome;
    }



}
